package com.yunfei.api.v2.qiuzhi.service.impl;

import com.yunfei.api.v2.qiuzhi.contants.QiuZhiContants;
import com.yunfei.api.v2.qiuzhi.service.QiuZhiTiWenService;
import com.yunfei.api.v2.user.valid.TiWenValid;
import com.yunfei.api.v2.user.valid.TokenForm;
import com.yunfei.bo.SkQuesDtlBo;
import com.yunfei.common.json.result.ResultModel;
import com.yunfei.core.utils.TokenUtil;
import com.yunfei.core.utils.enmu.QuesResultStatus;
import com.yunfei.core.utils.enmu.ResultStatus;
import com.yunfei.core.utils.id.IdUtils;
import com.yunfei.core.utils.string.StringUtil;
import com.yunfei.mapper.aiiidb.SkQuesDtlMapper;
import com.yunfei.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: zntg_v1
 * @description:
 * @author: zhengyunfei
 * @create: 2018-10-30 15:48
 **/
@Service("qiuZhiTiWenService")
public class QiuZhiTiWenServiceImpl implements QiuZhiTiWenService {
    @Autowired
    private SkQuesDtlMapper skQuesDtlMapper;
    @Autowired
    private RedisService redisService;
    /**
     * @Description: 求知广场:提问
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:10:36
     */
    public ResultModel tiwen(TiWenValid requestParam, TokenForm tokenForm, ResultModel resultModel) {
        String quesTit=requestParam.getQuesTit();
        String token=tokenForm.getToken();
        if(StringUtil.isNullOrEmpty(token)){
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return resultModel;
        }
        if(token.length()<64){
            resultModel.setCode(ResultStatus.TOKEN_WUXIAO.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_WUXIAO.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(quesTit)){
            resultModel.setCode(QuesResultStatus.QUES_TIT_NOTNULL.getCode());
            resultModel.setMessage(QuesResultStatus.QUES_TIT_NOTNULL.getMessage());
            return resultModel;
        }
        if(redisService.exists(token)){
            SkQuesDtlBo skQuesDtlBo=new SkQuesDtlBo();
            String quesId= IdUtils.getId();
            skQuesDtlBo.setQuesId(quesId);
            skQuesDtlBo.setQuesTit(quesTit);
            String userId= TokenUtil.getInstance().getUserId(token);
            skQuesDtlBo.setUserId(userId);
            String remdFlg= QiuZhiContants.REMD_FLG_N;//推荐标识
            String hopgFlg=QiuZhiContants.HOPG_FLG_N;//首页展示标识
            String checkFlg=QiuZhiContants.CHECK_FLG_W;//审核标识
            skQuesDtlBo.setRemdFlg(remdFlg);
            skQuesDtlBo.setHopgFlg(hopgFlg);
            skQuesDtlBo.setCheckFlg(checkFlg);
            skQuesDtlMapper.add(skQuesDtlBo);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;

        }else{
            resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
            return resultModel;
        }
    }


}
